using UnityEngine;
using System.Collections;

public class Projectile : MonoBehaviour 
{
	// Represents the speed of the projectile
	public float ProjectileSpeed;
    public GameObject ExplosionPrefab;
	
	//-------- MEMBERS -------------//
	private Transform mTransform;
	Enemy enemy;
	
	//--------- GENERAL METHODS ---------- //
	// Use this for initialization
	void Start () 
	{
		// Get the reference to the enemy
		enemy = (Enemy)GameObject.Find("Enemy").GetComponent("Enemy");
		
		// For performance reasons, keep a reference of the transform. It will be
		// useful for mobile devices where each processor tick encounts.
		mTransform = transform;
	}
	
	// Update is called once per frame
	void Update () 
	{
		// Get the amount to move using the speed and the elapsed time
		float amountToMove = ProjectileSpeed * Time.deltaTime;
		
		// Add the amount to the position, applying it to the Y axis
		mTransform.Translate(Vector3.up * amountToMove);
		
		// Check if the projectile is leaving the screen
		if(mTransform.position.y >= 5.25f)
		{
			// Destroy it
			Destroy(this.gameObject);
		}
	}

    // Override the onTrigger event
    void OnTriggerEnter(Collider otherObject)
    {
        if (otherObject.tag == "enemy")
        {
            // Refresh the enemy position
            
            Instantiate(ExplosionPrefab, enemy.transform.position, enemy.transform.rotation);
			
			// Increase the max speed of the enemy for increasing the difficulty
			enemy.MaxSpeed += 1f;
			enemy.MinSpeed += 0.5f;
            enemy.SePositionAndSpeed();

            // Destroy the projectile
            Destroy(this.gameObject);
			
			// Increase the player score
			Player.Score += 100;
			
			if(Player.Score >= 1000)
			{
				// The player wins
				Application.LoadLevel(GameConstants.WIN_SCREEN);
			}
        }
    }
}
